package com.xinghen.security.browser;

import com.xinghen.security.browser.authentication.DefaultAuthenticationFailureHandler;
import com.xinghen.security.browser.authentication.DefaultAuthenticationSuccessHandler;
import com.xinghen.security.browser.validate.code.SessionValidateCodeRepository;
import com.xinghen.security.core.properties.SecurityProperties;
import com.xinghen.security.core.validate.code.ValidateCodeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 * @author xinghen
 * @ClassName: BrowserSecurityBeanConfig
 * @Package: com.xinghen.security.browser
 * @Description 浏览器环境下扩展点配置，配置在这里的bean，业务系统都可以通过声明同类型或同名的bean来覆盖安全
 * 模块默认的配置
 * @create 2018-04-19 10:28
 */
@Configuration
public class BrowserSecurityBeanConfig {

    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 默认的浏览器端认证失败处理器
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "browserAuthenticationFailureHandler")
    public AuthenticationFailureHandler authenticationFailureHandler() {
        return new DefaultAuthenticationFailureHandler(securityProperties);
    }

    /**
     * 默认的浏览器端认证成功处理器
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "browserAuthenticationSuccessHandler")
    public AuthenticationSuccessHandler authenticationSuccessHandler() {
        return new DefaultAuthenticationSuccessHandler(securityProperties);
    }

    /**
     * 默认的验证码存取器
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(ValidateCodeRepository.class)
    public ValidateCodeRepository validateCodeRepository() {
        return new SessionValidateCodeRepository();
    }

}
